package com.fc.mall.portal.service.impl;

import com.fc.mall.mapper.UmsMemberAccountChangeHistoryMapper;
import com.fc.mall.mapper.UmsMemberAccountMapper;
import com.fc.mall.model.*;
import com.fc.mall.model.*;
import com.fc.mall.portal.service.ConfigService;
import com.fc.mall.portal.service.UmsMemberAccountService;
import com.fc.mall.portal.service.UmsMemberService;
import com.fc.mall.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
public class UmsMemberAccountServiceImpl implements UmsMemberAccountService {
    @Autowired
    private UmsMemberAccountMapper memberAccountMapper;
    @Autowired
    private UmsMemberService memberService;

    @Autowired
    private ConfigService configService;

    @Autowired
    private UmsMemberAccountChangeHistoryMapper accountChangeHistoryMapper;

    public void initMemberAccount(UmsMember umsMember)
    {
        if(umsMember != null)
        {
            UmsMemberAccount memberAccount = new UmsMemberAccount();
            memberAccount.setMemberId(umsMember.getId());
            memberAccount.setAbleAmount(0L);
            memberAccount.setTotalAmount(0L);
            memberAccount.setWaitAmount(0L);
            memberAccountMapper.insert(memberAccount);
        }
    }

    @Override
    public UmsMemberAccount getMemberAccountInfo() {
        UmsMember curMember = memberService.getCurrentMember();
        UmsMemberAccountExample example = new UmsMemberAccountExample();
        example.createCriteria().andMemberIdEqualTo(curMember.getId());
        List<UmsMemberAccount> memberAccountList = memberAccountMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(memberAccountList))
        {

            UmsMemberAccount memberAccount = memberAccountList.get(0);
            memberAccount.setDescription(configService.getRequiredConfig(Constants.withdrawConfig.REMARK.getExternalCode()));
            return memberAccount;
        }


        return null;
    }

    @Override
    public void updateTotalAccount(Long memberId, Integer amount) {
        UmsMemberAccountExample example = new UmsMemberAccountExample();
        example.createCriteria().andMemberIdEqualTo(memberId);
        List<UmsMemberAccount> memberAccountList = memberAccountMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(memberAccountList))
        {
            UmsMemberAccount memberAccount = memberAccountList.get(0);
            // memberAccount.setTotalAmount(memberAccount.getTotalAmount() + amount);
            memberAccount.setWaitAmount(memberAccount.getWaitAmount() + amount);
            memberAccountMapper.updateByPrimaryKey(memberAccount);
        }

    }

    @Override
    public int changeMemberAccountToAble(String orderSn) {
        UmsMemberAccountChangeHistoryExample example = new UmsMemberAccountChangeHistoryExample();
        example.createCriteria().andOrderSnEqualTo(orderSn).andAccountTypeEqualTo(Constants.AccountType.WaitAccount.getValue());

        List<UmsMemberAccountChangeHistory> changeHistoryList = accountChangeHistoryMapper.selectByExample(example);
        if(null == changeHistoryList || changeHistoryList.isEmpty())
        {
            return 0;
        }
        for(UmsMemberAccountChangeHistory history : changeHistoryList)
        {
            // 将原有的未结状态设置为已结状态
            history.setStatus(0);
            accountChangeHistoryMapper.updateByPrimaryKey(history);


            UmsMemberAccountExample accountExample = new UmsMemberAccountExample();
            accountExample.createCriteria().andMemberIdEqualTo(history.getMemberId());
            List<UmsMemberAccount> memberAccountList = memberAccountMapper.selectByExample(accountExample);
            if(null == memberAccountList || memberAccountList.isEmpty())
            {
                continue;
            }
            UmsMemberAccount memberAccount = memberAccountList.get(0);
            memberAccount.setTotalAmount(memberAccount.getTotalAmount() + history.getAmount());
            memberAccount.setAbleAmount(memberAccount.getAbleAmount() + history.getAmount());
            memberAccount.setWaitAmount(memberAccount.getWaitAmount() - history.getAmount());
            memberAccountMapper.updateByPrimaryKeySelective(memberAccount);
            UmsMemberAccountChangeHistory newHistory = new UmsMemberAccountChangeHistory();
            newHistory.setMemberId(history.getMemberId());
            newHistory.setFromMemberId(history.getFromMemberId());
            newHistory.setType(history.getType());
            newHistory.setAmount(history.getAmount());
            newHistory.setRemark("佣金发放");
            newHistory.setOrderSn("");
            newHistory.setRate(history.getRate());
            newHistory.setAccountType(Constants.AccountType.MemberAccount.getValue());
            newHistory.setCreateTime(new Date());
            accountChangeHistoryMapper.insertSelective(newHistory);
        }
        return 0;
    }
}
